package com.coder4j.starter.web.util;

import com.coder4j.starter.tools.util.StringUtils;
import lombok.NonNull;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * Request工具类
 *
 * @author Keller
 * @date 2023-03-19 14:20:17
 */
public class RequestUtils {




    private static final String USER_AGENT = "User-Agent";

    private static final String UNKNOWN = "unknown";
    private static final String LOCALHOST_IP = "127.0.0.1";
    // 客户端与服务器同为一台机器，获取的 ip 有时候是 ipv6 格式
    private static final String LOCALHOST_IPV6 = "0:0:0:0:0:0:0:1";
    private static final String SEPARATOR = ",";

    private static final String[] headers = {
            "Proxy-Client-IP",
            "x-forwarded-for",
            "WL-Proxy-Client-IP",
            "X-Real-IP"
    };


    /**
     * Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36
     * @param request
     * @return
     */
    public static String getUserAgent(@NonNull HttpServletRequest request){
        return request.getHeader(USER_AGENT);
    }


    public static String getBrowser(@NonNull HttpServletRequest request){
        String userAgent = getUserAgent(request);
        if(StringUtils.isEmpty(userAgent)){
            return null;
        }
        int index = userAgent.lastIndexOf(")");
        if(index < 0){
            return null;
        }
        return userAgent.substring(index + 1);
    }

    public static String getOS(@NonNull HttpServletRequest request){
        String userAgent = getUserAgent(request);
        if(StringUtils.isEmpty(userAgent)){
            return null;
        }
        int index = userAgent.indexOf(")");
        if(index < 0){
            return null;
        }
        userAgent = userAgent.substring(0,index);
        index = userAgent.indexOf("(");
        if(index < 0){
            return userAgent;
        }
        userAgent = userAgent.substring(index + 1);

        index = userAgent.indexOf(";");

        return index < 0 ? userAgent : userAgent.substring(0,index);
    }


    // 根据 HttpServletRequest 获取 IP
    public static String getIpAddress(@NonNull HttpServletRequest request) {
        String ip = null;
        for (String header : headers) {
            ip = request.getHeader(header);
            if (StringUtils.notEmpty(ip) && !UNKNOWN.equalsIgnoreCase(ip)) {
                break;
            }
        }

        if (StringUtils.isEmpty(ip) || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
            if (LOCALHOST_IP.equalsIgnoreCase(ip) || LOCALHOST_IPV6.equalsIgnoreCase(ip)) {
                // 根据网卡取本机配置的 IP
                InetAddress iNet = null;
                try {
                    iNet = InetAddress.getLocalHost();
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                }
                if (iNet != null) {
                    ip = iNet.getHostAddress();
                }
            }
        }
        // 对于通过多个代理的情况，分割出第一个 IP
        if (ip != null && ip.length() > 15) {
            if (ip.indexOf(SEPARATOR) > 0) {
                ip = ip.substring(0, ip.indexOf(SEPARATOR));
            }
        }
        return LOCALHOST_IPV6.equals(ip) ? LOCALHOST_IP : ip;
    }
}
